package br.com.comanda.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;
import br.com.comanda.restclient.RestClient;
import br.com.comanda.util.JSONToVO;
import br.com.comanda.util.VOToJSON;
import br.com.comanda.util.WebServiceUtil;
import br.com.comanda.vos.Item;

public class ItemService {

	public void insertItem(Item item) {
		try {
			URL url = new URL(WebServiceUtil.URL + "items");

			HttpURLConnection conexao = (HttpURLConnection) url.openConnection();
			conexao.setRequestMethod("POST");
			conexao.addRequestProperty("Content-type", "application/json");

			conexao.setDoOutput(true);

			conexao.connect();

			OutputStream os = conexao.getOutputStream();

			Log.i("ITEM JSON", VOToJSON.toItemJSON(item).toString());

			os.write(VOToJSON.toItemJSON(item).toString().getBytes());
			os.flush();

			InputStream is = conexao.getInputStream();
			Log.i("RESTCLIENT POST", is.toString());

		} catch (IOException e) {
			Log.i("RESTCLIENT POST", e.getMessage());
		}
	}

	public List<Item> getItemsByAccount(int accountId) {
		RestClient client = new RestClient();
		String result = client.getRESTFileContent(WebServiceUtil.URL + "items/" + accountId);

		Log.i("INFO", result);

		List<Item> itemList = new ArrayList<Item>();
		try {
			JSONObject itemResult = new JSONObject(result);

			Item item = null;
			try {
				JSONArray itensJson = itemResult.getJSONArray("item");

				for (int i = 0; i < itensJson.length(); i++) {
					item = JSONToVO.toItem(itensJson.getString(i));
					itemList.add(item);
				}

			} catch (Exception e) {
				item = JSONToVO.toItem(itemResult.getString("item"));
				itemList.add(item);
			}

			return itemList;

		} catch (Exception e) {
			Log.i("ERROR", e.getMessage());
			return itemList;
		}
	}

	public void updateItem2(int id, int qtde) {
		RestClient client = new RestClient();
		client.put(WebServiceUtil.URL + "items/" + id + "/" + qtde);
	}

	public void updateItem(Item item) {
		try {
			URL url = new URL(WebServiceUtil.URL + "items");

			HttpURLConnection conexao = (HttpURLConnection) url.openConnection();
			conexao.setRequestMethod("PUT");
			conexao.addRequestProperty("Content-type", "application/json");

			conexao.setDoOutput(true);

			conexao.connect();

			OutputStream os = conexao.getOutputStream();
			os.write(VOToJSON.toItemJSON(item).toString().getBytes());
			os.flush();

			InputStream is = conexao.getInputStream();
			Log.i("RESTCLIENT PUT", is.toString());

		} catch (IOException e) {
			Log.i("RESTCLIENT ERROR PUT", e.getMessage());
		}
	}

	public void excluirItem(int id) {
		HttpClient httpClient = new DefaultHttpClient();
		HttpDelete httpDelete = new HttpDelete(WebServiceUtil.URL + "items/" + id);

		try {
			httpClient.execute(httpDelete);

		} catch (Exception e) {
			Log.e("CLIENT", "Falha ao acessar webservice");
		}

	}
}
